package cc.aiee.demo.springboot.controller.paypal;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cc.aiee.core.bean.R;
import cc.aiee.core.kit.KitId;
import cc.aiee.demo.springboot.basic.constant.ContentType;
import cc.aiee.demo.springboot.controller.paypal.bean.PayPalCreateQuery;
import cc.aiee.paypal.api.PayPalApi;
import cc.aiee.paypal.core.dto.PayPalRefundDTO;
import cc.aiee.paypal.core.dto.PayPalTransactionDTO;
import cc.aiee.paypal.model.PayPalRefundModel;
import cc.aiee.paypal.model.PayPalTransactionModel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/paypal")
//@Api(tags = "贝宝PayPal")
@Tag(name = "贝宝支付")
@Slf4j
public class PayPalController {

	@PostMapping("/token")
//	@ApiOperation(value = "获取token", notes = "获取token值,在移动端进行支付时使用")
	@Operation(summary = "获取token")
	public @ResponseBody String appTransReq() {
		PayPalApi api = new PayPalApi();
		return api.getToken();
	}

	@PostMapping(value = "/transaction", consumes = { ContentType.FORM, ContentType.JSON })
//	@ApiOperation(value = "获取token", notes = "获取token值,在移动端进行支付时使用")
	@Operation(summary = "创建交易")
	public @ResponseBody R<PayPalTransactionDTO> payment_method_nonce(
			@Parameter(description = "交易信息", required = true) @Valid  @RequestBody PayPalCreateQuery model) {
		PayPalApi api = new PayPalApi();
		PayPalTransactionModel model2 = model.model(new PayPalTransactionModel());

		model2.setOrderNo(KitId.timeId());

		log.info("PayPal Create : \n{}", model2.toJson(true, false));
		return api.create(model2);
	}

	@PostMapping(value = "/refundOrCancel", consumes = { ContentType.FORM, ContentType.JSON })
	@Operation(summary = "撤销扣款或退款")
	public @ResponseBody R<PayPalRefundDTO> refundOrCancel(PayPalRefundModel refund) {
		PayPalApi api = new PayPalApi();
		log.info("PayPal Refund Or Cancel : \n{}", refund.toJson(true, false));
		return api.refundOrCancel(refund);
	}

	@PostMapping(value = "/refund", consumes = { ContentType.FORM, ContentType.JSON })
	@Operation(summary = "退款")
	public @ResponseBody R<PayPalRefundDTO> refund(PayPalRefundModel refund) {
		PayPalApi api = new PayPalApi();
		log.info("PayPal Refund : \n{}", refund.toJson(true, false));
		return api.refund(refund);
	}
}
